﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Common.Entitys.Messages;
using Common.Utlis;

namespace Common.Utils
{
    public class ReceiveIPadFindMethod
    {
        private static Dictionary<string, MethodInfo> m_DictActions = new Dictionary<string, MethodInfo>();

        public Func<object[], object[]> func = null;

        private object appointClass;
        /// <summary>
        /// 寻找指定实体类中的命令方法
        /// </summary>
        /// <param name="actionMsg">所执行的方法</param>
        /// <param name="ClassName">指定类中的方法</param>
        public ReceiveIPadFindMethod(object obj, [CallerMemberName] string ClassName="")
        {
            if (string.IsNullOrWhiteSpace(ClassName)) return;
            appointClass = obj;
            Type type = obj.GetType();
            MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (MethodInfo methodInfo in methodInfos)
            {
                m_DictActions.TryAdd($"{ClassName}_{methodInfo.Name}", methodInfo);
            }
            MessageAggregator<ActionMsg>.Subscribe(OnActionMsg, ClassName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="func">这个委托用于某些操作</param>
        /// <param name="ClassName"></param>
        public ReceiveIPadFindMethod(object obj, Func<object[], object[]> func, [CallerMemberName] string ClassName = "")
        {
            if (string.IsNullOrWhiteSpace(ClassName))
                return;
            this.func = func;
            appointClass = obj;
            Type type = obj.GetType();
            MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (MethodInfo methodInfo in methodInfos)
            {
                m_DictActions.TryAdd($"{ClassName}_{methodInfo.Name}", methodInfo);
            }
            MessageAggregator<ActionMsg>.Subscribe(OnActionMsg, ClassName);
        }
        private void OnActionMsg(ActionMsg actionMsg)
        {
            try
            {
                //直接执行
                if (!m_DictActions.TryGetValue($"{actionMsg.ClassName}_{actionMsg.ActionName}", out MethodInfo methodInfo))
                {
                    return;
                }
                if (actionMsg.Obj == null)
                {
                    methodInfo.Invoke(appointClass, null);
                }
                else
                {
                    if (func != null)
                    {
                        func.Invoke(actionMsg.Obj);
                    }
                    
                    methodInfo.Invoke(appointClass, actionMsg.Obj);
                }
            }
            catch (Exception e)
            {
                
            }
           
        }
    }
}
